package com.tulingxueyuan.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tulingxueyuan.mall.common.BaseContext;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.AddCarDto;
import com.tulingxueyuan.mall.dto.CartItemStorkDto;
import com.tulingxueyuan.mall.modules.oms.model.OmsCartItem;
import com.tulingxueyuan.mall.modules.oms.service.OmsCartItemService;
import com.tulingxueyuan.mall.modules.pms.model.PmsSkuStock;
import com.tulingxueyuan.mall.modules.pms.service.PmsSkuStockService;
import com.tulingxueyuan.mall.modules.ums.model.UmsMember;
import com.tulingxueyuan.mall.modules.ums.service.UmsMemberService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/car")
@Api(tags = "CarController",description = "购物车")
public class CarController {
    @Autowired
    OmsCartItemService cartItemService;
    @Autowired
    HttpSession session;
    @Autowired
    UmsMemberService memberService;
    @Autowired
    PmsSkuStockService skuStockService;
    /**
     *
     * 添加购物车
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody AddCarDto addCarDto){
            Boolean result = cartItemService.add(addCarDto);
            return CommonResult.success(result);
    }

    /**
     *  获取购物车数量
     */
    @GetMapping("/products/sum")
    public CommonResult getCount(){
        UmsMember umsMember = memberService.getCurrentMember();
        QueryWrapper<OmsCartItem> qw = new QueryWrapper<>();
        qw.select("sum(quantity) as count").lambda().eq(OmsCartItem::getMemberId,umsMember.getId());
        Map<String, Object> map = cartItemService.getMap(qw);
//        System.out.println(map);
        if (map==null){
            return CommonResult.success(0);
        }else {
            return CommonResult.success(Integer.parseInt(map.get("count").toString()));
        }
//        Object count = map.get("count");
//        List<OmsCartItem> list = cartItemService.list(lqw);
//        int count=0;
//        for (int i = 0; i < list.size(); i++) {
//            OmsCartItem item = list.get(i);
//            count = count + item.getQuantity();
//        }

    }

    /**
     * 获取购物车列表
     */
    @GetMapping("/list")
    public CommonResult getCarList(){
        LambdaQueryWrapper<OmsCartItem> lqw = new LambdaQueryWrapper<>();
        UmsMember umsMember = memberService.getCurrentMember();
        lqw.eq(OmsCartItem::getMemberId,umsMember.getId());
        List<OmsCartItem> list = cartItemService.list(lqw);
        List<CartItemStorkDto> cartItemStorkDtoList = list.stream().map(e->{
            CartItemStorkDto cartItemStorkDto = new CartItemStorkDto();
            BeanUtils.copyProperties(e,cartItemStorkDto);
            Long productSkuId = cartItemStorkDto.getProductSkuId();
            PmsSkuStock skuStock = skuStockService.getById(productSkuId);
            cartItemStorkDto.setStock(skuStock.getStock()-skuStock.getLockStock());
            return cartItemStorkDto;
        }).collect(Collectors.toList());
        return CommonResult.success(cartItemStorkDtoList);
    }

    /**
     * 修改购物车数量
     */
    @PostMapping("/update/quantity")
    @Transactional
    public CommonResult updateQuantity(@RequestBody OmsCartItem cartItem){
        boolean result = cartItemService.updateQuantity(cartItem);
        return CommonResult.success(result);
    }

    /**
     * (批量)删除购物车中的商品
     */
    @PostMapping("/delete")
    public CommonResult deleteCar(@RequestBody Map map){
        String ids = map.get("ids").toString();
        Boolean result = cartItemService.deleteCar(ids);
         return CommonResult.success(result);
    }



}
